'''
Created on Oct 7, 2012

@author: jake
'''
#from animefreak import GrabAnimeFreak

import sys
import urllib
from resources.UI import UI
from resources.animefreak import GrabAnimeFreak
#from animefreak import GrabAnimeFreak

class updateArgs:

    def __init__(self, *args, **kwargs):
        for key, value in kwargs.iteritems():
            if value == 'None':
                kwargs[key] = None
            else:
                kwargs[key] = urllib.unquote_plus(kwargs[key])
        self.__dict__.update(kwargs)    
    
class Main:
    
    def __init__(self, checkMode = True):
        self.parseArgs()
        if checkMode:
            self.checkMode()
            
    def parseArgs(self):
        if (sys.argv[2]):
            exec "self.args = updateArgs(%s')" % (sys.argv[2][1:].replace('&', "',").replace('=', "='"))
        else:
            self.args = updateArgs(mode = 'None', url = 'None', name = 'None', fastbrowse = 'True')
        
    def checkMode(self):
        mode = self.args.mode
        print "[AnimeFreak] Mode is " + str(mode)
        
        if mode is None:
            UI().showCategories()
        elif mode is 'ALL':
            GrabAnimeFreak().listAllSeries()
        elif mode is 'A2Z':
            if self.args.fastbrowse is "True":
                GrabAnimeFreak().listAtoZ()
            else:
                GrabAnimeFreak(False).listAtoZ()
        elif mode is 'SHOWA2Z':
            url = self.args.url
            
            if self.args.fastbrowse is "True":
                GrabAnimeFreak().listSeriesByChar(url)
            else:
                GrabAnimeFreak(False).listSeriesByChar(url)
            
            
        elif mode is 'ANIMESERIE':
            url = self.args.url
            GrabAnimeFreak().listSerieEpisodes(url)
        elif mode is 'EPISODE':
            url = self.args.url
            GrabAnimeFreak().listEpisodeVideoLinks(url)
        elif mode is 'PLAYEPISODE':
            title = self.args.name
            url = self.args.url
            UI().playEpisode(title, url)
        elif mode is 'NOTSUPPORTEDHOST':
            url = self.args.url
            UI().showDialog(url)
        else:
            print "[AnimeFreak] ERROR IN MODE"
            

Main()
    